package com.lgd.warehousemanagement.Utils;


import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.TextAppearanceSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class StringUtil {

    public static String logicRight(String string,int digit){
        int length = string.length();
        if(string.length() < digit){
            for(int i = 0; i < digit-length;i++){
                string = "0" + string;
            }
        }
        return string;
    }


    public static boolean isEmptyStr(String str){
        if (null!=str&&""!=str&&!TextUtils.isEmpty(str)){
            return true;
        }else {
            return false;
        }
    }

    public static  Boolean isEmpty(Object type){
        if(null!=type && !"".equals(type)){
            return false;
        }
        return true;
    }

    public static boolean isEmpty(@Nullable CharSequence str) {
        if (str == null || str.length() == 0 || "".equals(str) || str.toString().trim().length() == 0) {
            return true;

        } else {
            return false;
        }
    }

    /**
     * 判断对象为空字符串或者为null，如果满足其中一个条件，则返回true
     *
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean isNullOrEmpty(Object obj) {
        boolean isEmpty = false;
        if (obj == null) {
            isEmpty = true;
        } else if (obj instanceof String) {
            isEmpty = ((String) obj).trim().isEmpty();
        } else if (obj instanceof Collection) {
            isEmpty = (((Collection) obj).size() == 0);
        } else if (obj instanceof Map) {
            isEmpty = ((Map) obj).size() == 0;
        } else if (obj.getClass().isArray()) {
            isEmpty = Array.getLength(obj) == 0;
        }
        return isEmpty;
    }
    /**
     * 检查 email输入是否正确 正确的书写格 式为 username@domain
     *
     * @param text
     * @return
     */
    public static boolean checkEmail(String text, int length) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*") && text.length() <= length;
    }

    /**
     * 检查电话输入 是否正确 正确格 式 012-87654321、0123-87654321、0123－7654321
     *
     * @param text
     * @return
     */
    public static boolean checkTelephone(String text) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches(
                "(0\\d{2,3}-\\d{7,8})|" +
                        "(0\\d{9,11})|" +
                        "(\\d{7})|" +
                        "(\\d{8})|" +
                        "(4\\d{2}\\d{7})|" +
                        "(4\\d{2}-\\d{7})|" +
                        "(4\\d{2}-\\d{3}-\\d{4})|" +
                        "(4\\d{2}-\\d{4}-\\d{3})");
    }

    public static <T> Boolean  isListEmpty(List<T> list){
        if(list != null && list.size() > 0){
            return false;
        }
        return true;
    }

    /**
     * 检查手机输入 是否正确
     *
     * @param text
     * @return
     */
    public static boolean checkMobilephone(String text) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("^1(3[0-9]|4[579]|5[0-35-9]|8[0-9]|7[015-8])\\d{8}$");
    }

    /**
     * 检查中文名输 入是否正确
     *
     * @param text
     * @return
     */
    public static boolean checkChineseName(String text, int length) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("^[\u4e00-\u9fa5]+$") && text.length() <= length;
    }

    /**
     * 检查字符串中是否有空格，包括中间空格或者首尾空格
     *
     * @param text
     * @return
     */
    public static boolean checkBlank(String text) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("^\\s*|\\s*$");
    }

    /**
     * 检查字符串是 否含有HTML标签
     *
     * @param text
     * @return
     */

    public static boolean checkHtmlTag(String text) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("<(\\S*?)[^>]*>.*?<!--\\1-->|<.*? />");
    }

    /**
     * 检查URL是 否合法
     *
     * @param text
     * @return
     */
    public static boolean checkURL(String text) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("[a-zA-z]+://[^\\s]*");
    }

    /**
     * 检查IP是否 合法
     *
     * @param text
     * @return
     */
    public static boolean checkIP(String text) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}");
    }



    /**
     * 检查QQ是否 合法，必须是数字，且首位不能为0，最长15位
     *
     * @param text
     * @return
     */

    public static boolean checkQQ(String text) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("[1-9][0-9]{4,13}");
    }

    /**
     * 检查邮编是否 合法
     *
     * @param text
     * @return
     */
    public static boolean checkPostCode(String text) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("[1-9]\\d{5}(?!\\d)");
    }

    /**
     * 检查身份证是 否合法,15位或18位(或者最后一位为X)
     *
     * @param text
     * @return
     */
    public static boolean checkIDCard(String text) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("\\d{15}|\\d{18}|(\\d{17}[x|X])");
    }

    /**
     * 检查输入是否 超出规定长度
     *
     * @param length
     * @param text
     * @return
     */
    public static boolean checkLength(String text, int length) {
        return ((StringUtil.isNullOrEmpty(text)) ? 0 : text.length()) <= length;
    }

    /**
     * 判断是否为数字
     * @param text
     * @return
     */
    public static boolean isNumber(String text) {
        if(StringUtil.isNullOrEmpty(text))return false;
        return text.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
    }


    /**
     * 找出两个数组中不相同的元素
     *
     * @param t1
     * @param t2
     * @return
     */
    public static <T> List<T> compare(List<T> t1, List<T> t2) {
        List<T> list2 = new ArrayList<T>();
        for (T t : t2) {
            if (!t1.contains(t)) {
                list2.add(t);
            }
        }
        return list2;
    }
///  String 类型 转换
    public  static   long  string2Long(String str){
        return   Long.parseLong(TextUtils.isEmpty(str)?"0":str);
    }

    public  static  int  string2Int(String str){
        return   Integer.parseInt(TextUtils.isEmpty(str)?"0":str);
    }

    public  static  double  string2Double(String str){
        return    Double.parseDouble(TextUtils.isEmpty(str)?"0.00":str);
    }

    public  static  float  string2Float(String str){
        return  Float.parseFloat(TextUtils.isEmpty(str)?"0.00":str);
    }

    public static String priceFormat(double d) {
        DecimalFormat df   = new DecimalFormat("######0.00");
        return  df.format(d);
    }
    public static String priceFormat(float d) {
        DecimalFormat df   = new DecimalFormat("######0.00");
        return  df.format(d);
    }

    //ForegroundColorSpan 文本颜色
    public static SpannableString  setForegroundColorSpan(String str, int color,int s,int e) {
        SpannableString spanString = new SpannableString(str);
        ForegroundColorSpan span_color = new ForegroundColorSpan(color);
//        StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);  //粗体、斜体等

        spanString.setSpan(span_color, s, e, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        spanString.setSpan(span, s,e, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        return  spanString;
    }

    // BackgroundColorSpan 文本背景色
    public static SpannableString setBackgroundColorSpan(String str, int color,int s,int e) {
        SpannableString spanString = new SpannableString(str);
        BackgroundColorSpan span = new BackgroundColorSpan(color);
        spanString.setSpan(span, s, e, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return spanString;
    }

    // StyleSpan 字体样式：粗体、斜体等
    public static  SpannableString setStyleSpan(String str, int color,int s,int e) {
        SpannableString spanString = new SpannableString(str);
        StyleSpan span = new StyleSpan(Typeface.BOLD);
        spanString.setSpan(span, s,e, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return spanString;
    }

    // RelativeSizeSpan  相对大小
    public static void setRelativeFontSpan(String str, int color,int s,int e,float fontSize) {
        SpannableString spanString = new SpannableString(str);
        spanString.setSpan(new RelativeSizeSpan(fontSize),s, e, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    }

    // TypefaceSpan   文本字体
    public static SpannableString setTypefaceSpan(String str, String typefaceSpan,int color,int s,int e) {
        SpannableString spanString = new SpannableString(str);
        spanString.setSpan(new TypefaceSpan(typefaceSpan), s, e, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return spanString;
    }

    // URLSpan 文本超链接
    public static  SpannableString addUrlSpan(String str,String url,int s,int e) {
        SpannableString spanString = new SpannableString(str);
        URLSpan span = new URLSpan(url);
        spanString.setSpan(span, s ,e, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return  spanString;
    }

    // ImageSpan  图片
    public static SpannableString addImageSpan(String str, Drawable drawable, int s, int e) {
        SpannableString spanString = new SpannableString(str);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
        spanString.setSpan(span, s, e, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return  spanString;
    }

    // UnderlineSpan  下划线
    public static SpannableString addUnderLineSpan(TextView tv, String str) {
        SpannableString spanString = new SpannableString(str);
        UnderlineSpan span = new UnderlineSpan();
        spanString.setSpan(span, 0, str.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return  spanString;
    }

    // StrikethroughSpan 删除线
    public static SpannableString addStrikeSpan(String str,int s,int e) {
        SpannableString spanString = new SpannableString(str);
        StrikethroughSpan span = new StrikethroughSpan();
        spanString.setSpan(span, s, e, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return  spanString;
    }

    // TextAppearanceSpan 文本外貌（包括字体、大小、样式和颜色）
    public static SpannableString setTextAppearanceSpan(String str, TextAppearanceSpan textAppearanceSpan, int s, int  e) {
        SpannableString spanString = new SpannableString(str);
        spanString.setSpan(textAppearanceSpan, s, e, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return  spanString;
    }



}
